let request = require("request");
let fs = require("fs");
let province = require("./province.js");
let city = require("./city.js");

let getCtiy = (parentId, callback) => {
    request({
        method: "get",
        url: `https://ali-city.showapi.com/areaDetail?parentId=${parentId}`,
        headers: {
            "Authorization": "APPCODE 35d84f37bb50454fb2a6bc7786cb8e10"
        }
    }, (err, res, body) => {
        typeof callback == "function" ? callback(JSON.parse(body)) : "";
    })
};
let arr = [];
let dg = (num) => {
    // let temp = province[num];//处理市
    let temp = city[num];//处理区县
    num++;
    getCtiy(temp.id, res => {
        arr = [...arr, ...res.showapi_res_body.data || []];
        // if (num < province.length) {//处理市
        if (num < city.length) {//处理区县
            dg(num);
        } else {
            // console.log(arr);
            // fs.writeFile('./json/city.json', JSON.stringify(arr),function(err){//写入市
            fs.writeFile('./json/region.json', JSON.stringify(arr), function (err) {//写入区县
                console.error(err);
            });
        }
    });
};
dg(0);
// console.log(city);
